package com.celan.year2023.month05.day01;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @author Celan
 * @ClassName Solution
 * @description: TODO
 * @date 2023/05/01
 * @version: 1.0
 */
public class Solution {
    HashMap<Integer, List<Integer>> map;
    int[] informTime;
    int max = 0;

    void dfs(int idx, int sum) {
        if (!map.containsKey(idx)) {
            //底层员工
            max = Math.max(max, sum);
            return;
        }

        for (int emp : map.get(idx)) {
            dfs(emp, sum + informTime[emp]);
        }
    }

    public int numOfMinutes(int n, int headID, int[] manager, int[] informTime) {
        this.informTime = informTime;
        map = new HashMap<>();
        int boss = -1;
        for (int i = 0; i < n; i++) {
            if (manager[i] == -1) {
                boss = i;
                continue;
            }
            if (!map.containsKey(manager[i])) {
                ArrayList<Integer> emp = new ArrayList<>();
                emp.add(i);
                map.put(manager[i], emp);
            } else {
                map.get(manager[i]).add(i);
            }
        }
        dfs(boss, informTime[boss]);
        return max;
    }

    public static void main(String[] args) {
        Solution solution = new Solution();
        //6
        //2
        //[2,2,-1,2,2,2]
        //[0,0,1,0,0,0]
        solution.numOfMinutes(6, 2, new int[]{2, 2, -1, 2, 2, 2}, new int[]{0, 0, 1, 0, 0, 0});
    }
}

